package panel.recommendPathInfo
{
	import assists.Lang;
	import assists.URI;
	import assists.resource.ImageResource;
	import assists.resource.ResourceLoader;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import struct.RecommendPath;
	
	public class PathInfoList extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var info:RecommendPath;
		private var img:ImageResource;
		private var descBox:DescBox;
		private var boxList:Array;
		
		private var boxContainer:Sprite;
		private var onShowDetail:Function;
		
		private var startPoint:Point;
		
		public function PathInfoList(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI)
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			boxList = [];
			addEvent();
		}
		
		public function changeData(obj:RecommendPath):void
		{
			if(obj){
				
				descBox = new DescBox(_stage,_loader,_lang,_uri);
				descBox.repo = repo;
				
				boxContainer = new Sprite;
				descBox.changeData(obj.desc);
				img = new ImageResource(_loader,obj.url,reposition);
				
				
				for(var i:int = 0;i < obj.info.length;i++){
					var infoBox:PathInfoBox = new PathInfoBox(_stage,_loader,_lang,_uri);
					infoBox.onRepo = adjustBoxLocation;
					infoBox.titleString = obj.name + "-" + _lang.client("第几天",{num:i + 1});
					
					//					boxContainer.addChild(infoBox);
					infoBox.x = 0;
					
					boxList.push(infoBox);
					infoBox.changeData(obj.info[i],i);
				}
			}
		}
		
		private function adjustBoxLocation():void
		{
			var hei:Number = 0;
			
			for(var i:int = 0;i < boxList.length;i++){
				boxList[i].y = hei;
				hei = boxList[i].y + boxList[i].height + 5;
			}
		}
		
		private function repo():void
		{
			boxContainer.y = _stage.stageHeight * .4 + descBox.height;
		}
		
		private function reposition():void
		{
			this.addChild(img);
			this.addChild(descBox);
			this.addChild(boxContainer);
			
			img.x = 0;
			img.y = 0;
			img.width = _stage.stageWidth;
			img.height = _stage.stageHeight * .4;
			
			descBox.x = 0;
			descBox.y = img.height + img.y;
			
			boxContainer.x = 0;
			boxContainer.y = img.y + img.height + descBox.height;
			
			for(var j:int = 0;j < boxList.length;j++){
				boxContainer.addChild(boxList[j]);
			}
		}
		
		public function set showDetail(func:Function):void
		{
			onShowDetail = func;
		}
		
		public function clear():void
		{
			removeEvent();
			
			descBox.clear();
			descBox = null;
			for(var j:int = 0;j < boxList.length;j++){
				boxList[j].clear();
			}
		}
		
		private function removeEvent():void
		{
			this.removeEventListener(MouseEvent.MOUSE_DOWN,downHandler);
			this.removeEventListener(MouseEvent.MOUSE_UP,upHandler);
		}
		
		private function addEvent():void
		{
			this.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,upHandler);
		}
		
		protected function upHandler(e:MouseEvent):void
		{
			var loc:Point = new Point(e.stageX,e.stageY);
			localToGlobal(loc);
			
			if(e.target.parent is PathInfoBox && checkDistance(loc,startPoint)){
				
				e.stopImmediatePropagation();
				if(onShowDetail is Function){
					onShowDetail(e.target.parent.data,e.target.parent.titleString);
				}
			}		
			startPoint = null;
		}
		
		
		private function checkDistance(p1:Point,p2:Point):Boolean
		{
			if(p2 != null){
				var dis:Number = 0;
				dis = Math.sqrt(Math.abs(Math.pow(p1.x - p2.x,2) + Math.pow(p1.y - p2.y,2)));
				var bool:Boolean;
				
				if(dis > (_stage.stageHeight * .01)){
					return false;
				}
				return true;
			}
			return false;
		}
		
		protected function downHandler(e:MouseEvent):void
		{
			e.stopImmediatePropagation();
			
			if(e.target.parent is PathInfoBox){
				if(startPoint == null){
					startPoint = new Point(e.stageX,e.stageY);
				}else{
					startPoint.x = e.stageX;
					startPoint.y = e.stageY;
				}
				localToGlobal(startPoint);
			}
		}
	}
}